package com.hzsparrow.ai.controller;

import com.hzsparrow.ai.entity.ModelConfig;
import com.hzsparrow.ai.entity.PlatformSettings;
import com.hzsparrow.ai.service.ModelConfigService;
import com.hzsparrow.ai.service.PlatformSettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 平台设置管理控制器
 */
@RestController
@RequestMapping("/api/platform-settings")
public class PlatformSettingsController {

    @Autowired
    private PlatformSettingsService platformSettingsService;

    @Autowired
    private ModelConfigService modelConfigService;

    /**
     * 获取平台设置
     */
    @GetMapping
    public ResponseEntity<?> getPlatformSettings() {
        try {
            PlatformSettings settings = platformSettingsService.getPlatformSettings();
            
            // 获取可选的模型列表
            List<ModelConfig> chatModels = modelConfigService.getModelsByType("chat");
            List<ModelConfig> embeddingModels = modelConfigService.getModelsByType("embedding");
            
            Map<String, Object> response = new HashMap<>();
            response.put("settings", settings);
            response.put("chatModels", chatModels);
            response.put("embeddingModels", embeddingModels);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("获取平台设置失败: " + e.getMessage());
        }
    }

    /**
     * 更新平台设置
     */
    @PutMapping
    public ResponseEntity<?> updatePlatformSettings(@RequestBody PlatformSettings settings) {
        try {
            // 验证默认模型是否存在
            if (settings.getDefaultChatModelId() != null) {
                ModelConfig chatModel = modelConfigService.getModelConfig(settings.getDefaultChatModelId());
                if (chatModel == null || !"chat".equals(chatModel.getModelType())) {
                    return ResponseEntity.badRequest().body("指定的默认对话模型不存在或类型不正确");
                }
            }
            
            if (settings.getDefaultEmbeddingModelId() != null) {
                ModelConfig embeddingModel = modelConfigService.getModelConfig(settings.getDefaultEmbeddingModelId());
                if (embeddingModel == null || !"embedding".equals(embeddingModel.getModelType())) {
                    return ResponseEntity.badRequest().body("指定的默认嵌入模型不存在或类型不正确");
                }
            }
            
            PlatformSettings updated = platformSettingsService.updatePlatformSettings(settings);
            return ResponseEntity.ok(updated);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("更新平台设置失败: " + e.getMessage());
        }
    }

    /**
     * 获取默认对话模型
     */
    @GetMapping("/default-chat-model")
    public ResponseEntity<?> getDefaultChatModel() {
        try {
            PlatformSettings settings = platformSettingsService.getPlatformSettings();
            if (settings.getDefaultChatModelId() != null) {
                ModelConfig model = modelConfigService.getModelConfig(settings.getDefaultChatModelId());
                return ResponseEntity.ok(model);
            }
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("获取默认对话模型失败: " + e.getMessage());
        }
    }

    /**
     * 获取默认嵌入模型
     */
    @GetMapping("/default-embedding-model")
    public ResponseEntity<?> getDefaultEmbeddingModel() {
        try {
            PlatformSettings settings = platformSettingsService.getPlatformSettings();
            if (settings.getDefaultEmbeddingModelId() != null) {
                ModelConfig model = modelConfigService.getModelConfig(settings.getDefaultEmbeddingModelId());
                return ResponseEntity.ok(model);
            }
            return ResponseEntity.ok(null);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("获取默认嵌入模型失败: " + e.getMessage());
        }
    }
} 